package nl.hanze.designpatterns.Views.Ticket;

import nl.hanze.designpatterns.Controllers.TicketController;
import nl.hanze.designpatterns.Domain.TroubleTicket;
import nl.hanze.designpatterns.Models.MainModel;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Aron on 23-9-2014.
 */
public class TicketScreen extends JPanel implements ActionListener, ListSelectionListener, Observer {
    private JPanel panel1;
    private JLabel AsLabel;
    private JLabel UserLabel;
    private JButton logoutButton;
    private JList TicketList;
    private JTextPane DesciptionPane;
    private JTextField ReportedByField;
    private JButton submitChangesButton;
    private JButton newTicketButton;
    private JButton qnaButton;
    private JLabel EnteredByUserLabel;
    private JLabel EnteredOnLabel;
    private JLabel TicketIdLabel;


    private TicketController controller;
    private MainModel mainModel;

    public TicketScreen(TicketController controller, MainModel mainModel) {
        this.controller = controller;
        this.mainModel = mainModel;


        add(panel1);
        mainModel.addObserver(this);
        addListeners();
        TicketList.setModel(mainModel.getTicketList());
        updateValues();
    }

    private void updateTicket() {
        mainModel.selectedTicket.description = DesciptionPane.getText();
        mainModel.selectedTicket.callerName = ReportedByField.getText();
    }

    private void updateValues() {
        System.out.println("<TicketScreen>[setValues]");
        String loggedInUser = mainModel.getLoggedInUser();

        UserLabel.setText(loggedInUser);

        String ticketId = "#"+mainModel.selectedTicket.getId();
        String userName = ""+mainModel.selectedTicket.getUserName();
        String callerName = ""+mainModel.selectedTicket.getCallerName();
        String description = ""+mainModel.selectedTicket.getDescription();
        String dateTime = ""+mainModel.selectedTicket.getDateTime();

        TicketIdLabel.setText(ticketId);
        DesciptionPane.setText(description);
        ReportedByField.setText(callerName);
        EnteredByUserLabel.setText(userName);
        EnteredOnLabel.setText(dateTime);
    }

    private void addListeners(){
        //logout button
        logoutButton.setActionCommand("logout");
        logoutButton.addActionListener(this);

        //new ticket
        newTicketButton.setActionCommand("newTicket");
        newTicketButton.addActionListener(this);

        //save ticket
        submitChangesButton.setActionCommand("saveTicket");
        submitChangesButton.addActionListener(this);
        
        //QnA
        qnaButton.setActionCommand("QnA");
        qnaButton.addActionListener(this);

        //ticketlist
        TicketList.addListSelectionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch(e.getActionCommand())
        {
            case "logout":
                System.out.println("[ACTION]logout");
                this.controller.logout();
                break;
            case "newTicket":
                System.out.println("[ACTION]newTicket");
                this.controller.newTicket();
                break;
            case "saveTicket":
                System.out.println("[ACTION]saveTicket");
                updateTicket();
                this.controller.saveTicket();
                break;
            case "QnA":
                System.out.println("[ACTION]QnA");
                this.controller.toQnA();
                break;
            default:
                System.out.println("[ACTION]unknown: "+e.getActionCommand());
        }
    }
    @Override
    public void valueChanged(ListSelectionEvent e) {

        JList<TroubleTicket> list = (JList<TroubleTicket>) e.getSource();

        System.out.println("[SELECTED]list1: "+ list.getSelectedValue());

        controller.selectTicket(list.getSelectedValue());
    }

    @Override
    public void update(Observable o, Object arg) {
        if (arg instanceof String) {
            if (arg.equals("ticket")) {
                updateValues();
            }
        }

    }
}
